package com.fywsy.fyweb.repository;

import com.fywsy.fyweb.entity.Category;
import com.fywsy.fyweb.entity.Gongneng;
import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

@Repository
public interface GongnengRepository extends JpaRepository<Gongneng, Long> {


    List<Gongneng> findAllByEnableAndGameId(boolean enable, long gameId);

    List<Gongneng> findAllByGameId(long gameId);


    List<Gongneng> findAllByEnableAndGameIdOrderByWeightDescIdAsc(boolean enable, long gameId);

    List<Gongneng> findAllByEnableAndReqAndGameIdOrderByWeightDesc(boolean enable, boolean req, long gameId);

    @Modifying
    @Transactional
    @Query(value = "DELETE FROM gongneng WHERE game_id = :game_id AND gid NOT IN :gids", nativeQuery = true)
    void deleteByGameIdAndGidNotIn(@Param("game_id") long gameId, @Param("gids") Set<Long> gids);


    @Modifying
    @Transactional
    @Query(value = "DELETE FROM gongneng WHERE game_id = :game_id", nativeQuery = true)
    void deleteByGameId(@Param("game_id") long gameId);


    List<Gongneng> findAllByGidIn(Set<Long> ids);

    Gongneng findFirstByGid(long gid);
}